package com.lemon.security.service;

import com.lemon.core.enums.ServiceEnum;
import com.lemon.core.exception.PermissionException;
import com.lemon.core.web.Request;
import com.lemon.core.web.Result;
import com.lemon.redis.service.RedisService;
import com.lemon.security.dto.LoginUserDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 李猛
 * @datetime 2024/2/7 23:27
 * @description 类对象
 */
@Slf4j
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private RedisService redisService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Result<?> result = Request.get(ServiceEnum.SYSTEM, "user/getByUsername?username=" + username, Result.class, false);
        if (!result.isSuccess()) {
            throw new PermissionException(result.getMessage());
        }
        return redisService.getByKey(username, LoginUserDetail.class);
    }
}
